<?php
class KeyGenerator
{
    const KEY_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';

    /**
     * Generates random solid key.
     *
     * @param int $length key length
     * @return string
     */
    public static function generateSolidKey($length = 6)
    {
        return self::generateKey(str_repeat('X', $length));
    }

    /**
     * Generates random key for specified map.
     *
     * @param string $keyMap
     * @param char $replacedChar char in $keyMap that will be replaced to random char
     * @param string $randomChars
     * @return string
     */
    public static function generateKey($keyMap, $replacedChar = 'X')
    {
        $randomChars = self::KEY_CHARS;
        $countRandomChars = strlen($randomChars);

        $key = "";
        for ($i = 0; $i < strlen($keyMap); ++$i)
        {
            $char = $keyMap[$i];

            if ($char == $replacedChar)
            {
                $charIndex = mt_rand(0, $countRandomChars - 1);
                $char = $randomChars[$charIndex];
            }

            $key .= $char;
        }

        return $key;
    }
}
?>